//诈骗易感人群案例
//号码注册时间长且使用老旧机型，可能为老年人
MATCH (u:User)-[has:has]->(p:Phone)-[used:use_device]->(d:Device)
WHERE  date(p.activation_date) <= date() - duration({months: 12*9})
AND d.release_year <= 2019

//被叫占比高
WITH u, has, p,
  count{(p)-[]-(:Phone)} AS callCnt,
  count{(p)-[]->(:Phone)} AS calltoCnt,
  count{(p)<-[]-(:Phone)} AS callfromCnt
WHERE calltoCnt * 100 / callCnt < 20
AND callCnt < 150

order by rand() limit 5

//被诈骗高风险号码呼叫
MATCH (p)-[short_forien_call:called]-(op:Phone{high_risk:TRUE})
WHERE short_forien_call.call_duration_minutes < 10

WITH u, has, p,
  collect(short_forien_call) AS cs,
  collect(op) AS ops,
  callCnt,
  calltoCnt,
  callfromCnt,
  count(short_forien_call) AS shortForienCnt 


// 构建子图结构
WITH u, p, 
  [p] + ops AS allNodes,  // 所有节点：用户手机 + 关联手机
  [has] + cs AS allRels,  // 所有关系：拥有关系 + 通话关系
  callCnt,
  calltoCnt,
  callfromCnt,
  shortForienCnt
match (p)-[use:use_device]->(dall:Device)
match (u)-[l:lives_in*1..2]-(p2)
// 返回用户及其子图
RETURN {
      oldDevice: TRUE,
      callCount: callCnt,
      shortForeignCallCount: shortForienCnt,
      callToPercentage: calltoCnt * 100 / callCnt,
      shortForeignCallPercentage: shortForienCnt * 100 / callCnt
    } as stats, u as orange_u, p as orange_p, allNodes, allRels, dall, use, l, p2